﻿using System;
using System.Diagnostics;
using System.Drawing;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XXPic
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {

        private readonly Stopwatch _stopwatch = new Stopwatch();
        private long _frameCount;
        private Bitmap _currentImage;
        private bool _isRunning;
        public Form1()
        {
            InitializeComponent();
            DoubleBuffered = true;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            _currentImage = GenerateTestImage(pictureBox.Width, pictureBox.Height);
            pictureBox.Image = _currentImage;
        }

        private async void btnStart_Click(object sender, EventArgs e)
        {
            if (_isRunning) return;

            _isRunning = true;
            btnStart.Enabled = false;
            _frameCount = 0;
            _stopwatch.Restart();

            // 在后台线程运行测试
            await Task.Run(() =>
            {
                while (_isRunning)
                {
                    // 生成新图像
                    var newImage = GenerateTestImage(pictureBox.Width, pictureBox.Height);

                    // 在UI线程更新
                    Invoke((Action)(() =>
                    {
                        // 释放旧图像资源
                        if (_currentImage != null)
                        {
                            _currentImage.Dispose();
                        }

                        _currentImage = newImage;
                        pictureBox.Image = _currentImage;
                        _frameCount++;

                        // 每秒更新一次帧率显示
                        if (_stopwatch.ElapsedMilliseconds >= 1000)
                        {
                            Text = $"图像加载测试 | FPS: {_frameCount}";
                            _frameCount = 0;
                            _stopwatch.Restart();
                        }
                    }));
                }
            });
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            _isRunning = false;
            btnStart.Enabled = true;
        }

        // 生成测试图像（避免磁盘IO影响测试结果）
        private Bitmap GenerateTestImage(int width, int height)
        {
            var bmp = new Bitmap(width, height);
            var rnd = new Random();

            using (var g = Graphics.FromImage(bmp))
            {
                // 生成随机渐变背景
                var color1 = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
                var color2 = Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256));
                var brush = new System.Drawing.Drawing2D.LinearGradientBrush(
                    new Point(0, 0),
                    new Point(width, height),
                    color1, color2);

                g.FillRectangle(brush, 0, 0, width, height);

                // 绘制随机图形
                for (int i = 0; i < 100; i++)
                {
                    var pen = new Pen(Color.FromArgb(rnd.Next(256), rnd.Next(256), rnd.Next(256)),
                                rnd.Next(1, 5));
                    g.DrawEllipse(pen,
                        rnd.Next(width), rnd.Next(height),
                        rnd.Next(20, 100), rnd.Next(20, 100));
                }
            }

            return bmp;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _currentImage?.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
